"""Codex审查结果的报告解析器。"""

import re
from typing import Dict, List, Optional

# Import data models
try:
    from .data_models import ParsedReport
except ImportError:
    from data_models import ParsedReport


class ReportParser:
    """解析Codex审查的report.md文件。"""

    @staticmethod
    def parse_report(report_content: str) -> ParsedReport:
        """解析report.md内容为结构化数据。

        Args:
            report_content: report.md的原始内容

        Returns:
            包含结构化数据的ParsedReport实例
        """
        if not report_content or not report_content.strip():
            return ParsedReport(
                status="unknown",
                issues=[],
                suggestions=[],
                raw_content=report_content or ""
            )

        status = ReportParser._extract_status(report_content)
        issues = ReportParser._extract_issues(report_content)
        suggestions = ReportParser._extract_suggestions(report_content)

        return ParsedReport(
            status=status,
            issues=issues,
            suggestions=suggestions,
            raw_content=report_content
        )

    @staticmethod
    def _extract_status(content: str) -> str:
        """从报告中提取审查状态。

        Returns:
            "approved", "major_issues", "minor_issues"或"unknown"
        """
        # Remove BOM if present
        content = content.lstrip('\ufeff')

        # Match "## Overall Assessment" followed by status on next line
        # Pattern: ## Overall Assessment\nSTATUS
        match = re.search(r'##\s+Overall\s+Assessment\s*\n\s*([A-Z_]+)', content, re.IGNORECASE)
        if match:
            status = match.group(1).strip().upper()
            if status == "APPROVED":
                return "approved"
            elif status == "MAJOR_ISSUES":
                return "major_issues"
            elif status == "MINOR_ISSUES":
                return "minor_issues"

        return "unknown"

    @staticmethod
    def _extract_issues(content: str) -> List[Dict[str, str]]:
        """从报告中提取问题列表。

        Returns:
            {"priority": "P0/P1/P2", "description": "..."}的列表
        """
        issues = []

        # Find "## Issue List" section
        match = re.search(r'##\s*Issue\s*List\s*\n(.*?)(?=\n##|\Z)', content, re.DOTALL | re.IGNORECASE)
        if not match:
            return issues

        issues_section = match.group(1)

        # Extract issues: - [P0] description or - [P1] file:line - description
        pattern = r'-\s*\[([P0-2])\]\s*(.+)'
        for issue_match in re.finditer(pattern, issues_section):
            priority = issue_match.group(1).strip()
            description = issue_match.group(2).strip()

            # Skip empty entries
            if description.lower() in ['', 'none', 'n/a']:
                continue

            issues.append({
                "priority": priority,
                "description": description
            })

        return issues

    @staticmethod
    def _extract_suggestions(content: str) -> List[str]:
        """从报告中提取建议列表。

        Returns:
            建议字符串列表
        """
        suggestions = []

        # Find "## Improvement Suggestions" section
        match = re.search(r'##\s*Improvement\s*Suggestions\s*\n(.*?)(?=\n##|\Z)', content, re.DOTALL | re.IGNORECASE)
        if not match:
            return suggestions

        suggestions_section = match.group(1)

        # Extract suggestions: - suggestion
        pattern = r'-\s*(.+)'
        for sug_match in re.finditer(pattern, suggestions_section):
            suggestion = sug_match.group(1).strip()

            # Skip empty entries
            if suggestion.lower() in ['', 'none', 'n/a']:
                continue

            suggestions.append(suggestion)

        return suggestions
